// pages/mine/register/index.js
const app = getApp();
var serverUrl = app.globalData.serverUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disbutton:false,
    eyeclose: 1,
    eyeUrl: "/asset/img/eye-hide.png",
    eye_open: "/asset/img/eye-show.png",
    eye_close: "/asset/img/eye-hide.png",
    eye_type: "password",
    getcodemess: "获取验证码",
    number: "",
    disbtn:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 密码显示点击事件 
   */
  eye: function (e) {
    var that = this;
    let id = e.currentTarget.id;
    if (id == 1) {
      that.setData({
        eyeclose: 0,
        eyeUrl: that.data.eye_open,
        eye_type: "text"
      })
    } else {
      that.setData({
        eyeclose: 1,
        eyeUrl: that.data.eye_close,
        eye_type: "password"
      })
    }
  },

  /**手机号码赋值操作**/
  setMobile: function (e) {
    this.setData({
      number: e.detail.value
    })
  },

  /**获取验证码**/
  getCode: function () {
    var that = this;
    that.setData({
      disbtn: true
    })
    var number = that.data.number;
    let v = /^1[3456789]\d{9}$/;
    let ev = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    if (!number || (!v.test(number) && !ev.test(number) ) ) {
      wx.showToast({
        title: '手机号码或邮箱格式错误',
        icon: 'none',
        duration: 1500
      });
      that.setData({
        disbtn: false
      })
      return false;
    }
    var i = 60;
          var timer = setInterval(function () {
            if (i <= 60 && i > 0) {
              i--;
              that.setData({
                getcodemess: i + "秒"
              })
            } else {
              i = 60;
              that.setData({
                getcodemess: "获取验证码",
                disbtn: false
              })
              clearInterval(timer);
            }
          }, 1000)
    // wx.request({
    //   url: serverUrl+'api/v1/getCode',
    //   method:"GET",
    //   data:{mobile:number},
    //   dataType:"json",
    //   header: {
    //     //设置参数内容类型为x-www-form-urlencoded
    //     'content-type': 'application/x-www-form-urlencoded',
    //     'Accept': 'application/json'
    //   },
    //   success:function(e){
    //     if(e.data.code == 200){
    //       var i = 60;
    //       var timer = setInterval(function () {
    //         if (i <= 60 && i > 0) {
    //           i--;
    //           that.setData({
    //             getcodemess: i + "秒"
    //           })
    //         } else {
    //           i = 60;
    //           that.setData({
    //             getcodemess: "获取验证码",
    //             disbtn: false
    //           })
    //           clearInterval(timer);
    //         }
    //       }, 1000)
    //     } else {
    //       wx.showToast({
    //         title: e.data.message,
    //         icon: 'none',
    //         duration: 1500
    //       });
    //       that.setData({
    //         disbtn: false
    //       })
    //     }
    //   },
    //   error:function(e){
    //     wx.showToast({
    //       title: "服务器错误",
    //       icon: 'none',
    //       duration: 1500
    //     });
    //     that.setData({
    //       disbtn: false
    //     })
    //   }
    // })
  },

  /**注册**/
  subRegister:function(data){
    var that = this;
    that.setData({
      disbutton:true
    })
    let detail = data.detail.value;
    if(!detail.nickname || detail.nickname.length <= 0 || detail.nickname.length > 10){
      that.setData({
        disbutton: false,
      })
      wx.showToast({
        title: "昵称不可少于一个字符且不可超过10个字符",
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    let mv = /^1[3456789]\d{9}$/;
    let ev = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
    if (!detail.number || (!mv.test(detail.number) && !ev.test(detail.number))){
      that.setData({
        disbutton: false,
      })
      wx.showToast({
        title: "邮箱或手机号码格式错误",
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    let pv = /^[a-zA-Z0-9_]{6,18}$/;
    if(!detail.password || !pv.test(detail.password)){
      that.setData({
        disbutton: false,
      })
      wx.showToast({
        title: "密码不能为空,且只能包含数字字母下划线,6-18位字符",
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    if(!detail.code || !/^\d{4}$/.test(detail.code)){
      that.setData({
        disbutton: false,
      })
      wx.showToast({
        title: "请输入验证码且验证码为4位数字",
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    let location = wx.getStorageSync('location');
    if (location) {
      detail.lat = location.lat;
      detail.lon = location.lon;
    }
    wx.request({
      url: serverUrl +'api/v1/users/registeries',
      data:detail,
      dataType:"json",
      method:"POST",
      success:function(data){
        var data = data.data;
        if ( data.code == 200 ) {
          wx.showToast({
            title: "恭喜您注册成功",
            icon: 'success',
            duration: 1500
          });
          let userInfo = {};
          userInfo.mobile = data.data.mobile;
          userInfo.number = data.data.number;
          userInfo.nickname = data.data.nickname;
          userInfo.uid = data.data.id;
          wx.setStorageSync("userInfo", userInfo);
          setTimeout(function(){
            wx.switchTab({
              url: '../index/index',
            })
          }, 1000)
        } else {
          that.setData({
            disbutton: false,
          })
          wx.showToast({
            title: data.message,
            icon: 'none',
            duration: 1500
          });
        }
      },
      error:function(data){
        that.setData({
          disbutton: false,
        })
        wx.showToast({
          title: "服务器错误!注册失败",
          icon: 'none',
          duration: 1500
        });
      }
    })
  }
})